﻿using Aweber.Entity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Aweber;
using System.Configuration;
using System.Collections.Generic;

namespace UnitTests
{


    /// <summary>
    ///This is a test class for SubscriberTest and is intended
    ///to contain all SubscriberTest Unit Tests
    ///</summary>
    [TestClass()]
    public class SubscriberTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Subscriber Constructor
        ///</summary>
        [TestMethod()]
        public void SubscriberConstructorTest()
        {
            MockAdapter adapter = null;
            Subscriber target = new Subscriber(adapter);
            Assert.IsNotNull(target);
        }

        /// <summary>
        ///A test for Save
        ///</summary>
        [TestMethod()]
        public void CreateSaveDeleteTest()
        {
            string appId = ConfigurationManager.AppSettings["AweberAppId"];
            API api = new API(appId, new MockAdapter());
            api.process_appId_token(ConfigurationManager.AppSettings["AweberToken"]);

            Account account = api.getAccount();

            BaseCollection<Subscriber> target = account.lists().entries[0].subscribers();

            SortedList<String, Object> parameters = new SortedList<string, object>();

            String email = "testingz40@testingz30.com";
            String name = "Adam Pedley";
            String misc = "";

            parameters.Add("email", email);
            parameters.Add("name", name);

            Subscriber subscriber = new Subscriber(api.adapter);
            
            subscriber.load_from_url("https://api.aweber.com/1.0/accounts/571075/lists/2114998/subscribers/446105138");

            Assert.IsNotNull(subscriber);

            subscriber.misc_notes = misc;

            // PLEASE NOTE: I would break point here. You can not do a modify unless the subscriber is confirmed
            // Hence I pause here. Wait for the email, accept, then proceed and all works as planned.
            // subscriber.Save();

            String self_link = subscriber.self_link;

            Subscriber newSubscriber = new Subscriber(api.adapter);

            newSubscriber.load_from_url(self_link);

            Assert.AreEqual(email, newSubscriber.email);
            Assert.AreEqual(name, newSubscriber.name);
            Assert.AreEqual(misc, newSubscriber.misc_notes);



        }

        /// <summary>
        ///A test for Save
        ///</summary>
        [TestMethod()]
        public void CheckDirtyTest()
        {

            Subscriber subscriber = new Subscriber(new MockAdapter());

            Assert.IsTrue(subscriber.get_dirty().Count == 0);

            subscriber.ad_tracking = "something";
            subscriber.custom_fields = "somthingelse";
            subscriber.email = "changed";
            subscriber.last_followup_message_number_sent = 2;
            subscriber.misc_notes = "misc notes";
            subscriber.name = "New Name";
            subscriber.status = "New Status";

            Assert.IsTrue(subscriber.get_dirty().Contains("ad_tracking"));
            Assert.IsTrue(subscriber.get_dirty().Contains("custom_fields"));
            Assert.IsTrue(subscriber.get_dirty().Contains("email"));
            Assert.IsTrue(subscriber.get_dirty().Contains("last_followup_message_number_sent"));
            Assert.IsTrue(subscriber.get_dirty().Contains("misc_notes"));
            Assert.IsTrue(subscriber.get_dirty().Contains("name"));
            Assert.IsTrue(subscriber.get_dirty().Contains("status"));

            subscriber.clear_dirty();

            Assert.IsTrue(subscriber.get_dirty().Count == 0);


        }

    }
}
